<?php
//专题文件
class Controller_Admin_Topic extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'topic';
        parent::__construct($req, $res);

        $this->_list_where = array(
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );

        $this->_shopId = App_Shop_Manager::currentShopId();
        if ($this->_shopId > 0)
            $this->_list_where[] = array('shopId', '=', $this->_shopId);
    }

	//显示专题列表
    public function action_index ()
    {
        $this->action_list();
    }

	//创建专题
    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

	//修改专题
	//$nid int 专题id
    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
            $this->_form_data['image'] = explode("|", $data['image']);
        }

        parent::action_update($nid);
    }

	//专题详细信息
	//$nid int 专题id
    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 
        $this->_form_data = $data;
        $this->_form_data['image'] = explode("|", $data['image']);

        parent::action_detail($nid);
    }

	//重构POST数据
    private function rebuild_post_data (& $post)
    {
        $post['image'] = implode('|', $post['image']);
        $post['uid']   = user::current_user_id();
    }

    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            $rs['shopId'] = App_Shop::name($rs['shopId']);
        }
    }

	//显示商品列表
    public function actions_by_list_row (& $row, & $menus)
    {
	        $controller = Request::initial()->controller();
	        $directory  = Request::initial()->directory();
	
	        $directory  = empty($directory) ? '' : '/'.$directory;
	
	        $url  = "$directory/topicgoods/list/1?topicId=". $row['id'];
	        $link = "<a href=\"$url\">商品列表</a>";

            $menus[] = $link;
    }
}
